package com.bjesc_b2c.app.appraiset.mvp.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseFragmentActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.FragmentCarClue;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.RepulseHistoryFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.SourceEditFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.ToDayPlanFragment;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 主界面
 */
public class MainActivity extends BaseFragmentActivity implements RadioGroup.OnCheckedChangeListener {

    @InjectView(R.id.rb_today_plan)
    RadioButton todayPlanRb;
    @InjectView(R.id.rb_order_details)
    RadioButton orderDetailsRb;
    @InjectView(R.id.rb_upload_history)
    RadioButton uploadHistoryRb;
    @InjectView(R.id.rb_return_history)
    RadioButton returnHistoryRb;
    @InjectView(R.id.rg_group)
    RadioGroup indexRg;
    @InjectView(R.id.text_title)
    TextView titleTv;
    @InjectView(R.id.tv_addCar)
    TextView addCarTv;
    @InjectView(R.id.rela_title)
    RelativeLayout relativeTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        initViews();
    }
    private void initViews() {
        indexRg.setOnCheckedChangeListener(this);
        todayPlanRb.setChecked(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            showToast("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }
    @OnClick(R.id.tv_addCar)
    public void onClick(){
        startIntent(new Intent(MainActivity.this, AddCarSourceActivity.class),false);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        addCarTv.setVisibility(View.GONE);
        switch (checkedId) {
            //待检车源
            case R.id.rb_today_plan:
                switchContent(ToDayPlanFragment.class,R.id.fl_content);
                titleTv.setText("待检车源");
                addCarTv.setVisibility(View.VISIBLE);
                break;
            //车源编辑
            case R.id.rb_order_details:
                switchContent(SourceEditFragment.class,R.id.fl_content);
                titleTv.setText("车源编辑");
                break;
            //接单线索
            case R.id.rb_upload_history:
              switchContent(FragmentCarClue.class,R.id.fl_content);
                titleTv.setText("接单线索");
                break;
            //打回历史
            case R.id.rb_return_history:
                switchContent(RepulseHistoryFragment.class,R.id.fl_content);
                titleTv.setText("打回历史");
                break;
        }
    }

}
